<?php
/**
 * @desc 用户表数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class User extends Base
{
	//数据表
	protected $table = 'pert_user';
	//用户登录查询
	public function userLogin($username)  
    {
		 $table = $this->table;
		 $filed = "`user_id`,`username`,`password`,`nick_name`,`mobile`,`email`";
		 $sql = "select {$filed} from `{$table}` where `username` = :username and `delete_flag` = 0 and (`status` = 1 or `status` = 2)";
		 $binds = array(":username" => $username);
		 return $this->select_table($sql, $binds, Base::QUERY_RAW);
    }
	//用户注册
	public function userRegister(array $data){
		$table = $this->table;
		return $this->insert_table($data, $table);
	}
	//用户登录次数统计
	public function userLoginCount($username){
		$table = $this->table;
		$sql = "update `{$table}` set `login_count` = `login_count` + 1 where `username` = :username and `delete_flag` = 0 and `status` = 1";
		$binds = array(":username" => $username);
		return $this->update_table($sql, $binds);
	}
	//获取用户信息
	public function getUserInfo($userId){
		$table = $this->table;
		$filed = "`user_id`,`username`,`password`,`nick_name`,`mobile`,`email`";
		$sql = "select {$filed} from `{$table}` where `user_id` = :user_id and `delete_flag` = 0";
		$binds = array(":user_id" => $userId);
		return $this->select_table($sql, $binds, Base::QUERY_RAW);
	}
	//修改用户邮箱
	public function modifyEmail($userId, $email){
		$table = $this->table;
		$sql = "update `{$table}` set `email` = :email where `user_id` = :user_id and `delete_flag` = 0";
		return $this->update_table($sql, array(':email'=>$email, ':user_id' => $userId));
	}
	//修改用户手机号
	public function modifyMobile($userId, $mobile){
		$table = $this->table;
		$sql = "update `{$table}` set `mobile` = :mobile where `user_id` = :user_id and `delete_flag` = 0";
		return $this->update_table($sql, array(':mobile'=>$mobile, ':user_id' => $userId));
	}
	//修改用户昵称
	public function modifyNickname($userId, $nick_name){
		$table = $this->table;
		$sql = "update `{$table}` set `nick_name` = :nick_name where `user_id` = :user_id and `delete_flag` = 0";
		return $this->update_table($sql, array(':nick_name'=>$nick_name, ':user_id' => $userId));
	}
	//修改密码
	public function modifyPasswd($userId, $password){
		$table = $this->table;
		$sql = "update `{$table}` set `password` = :password where `user_id` = :user_id and `delete_flag` = 0";
		return $this->update_table($sql, array(':password'=>$password, ':user_id' => $userId));
	}
}
